package logic

import (
	"context"
	"encoding/json"
	"time"
	"video-joy/models/cache"
	"video-joy/models/mysql"

	"video-joy/userrpc/internal/svc"
	"video-joy/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoListLogic {
	return &VideoListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 视频列表
func (l *VideoListLogic) VideoList(in *userrpc.VideoListRequest) (*userrpc.VideoListResponse, error) {
	// todo: add your logic here and delete this line

	result, _ := cache.Rdb.Get(l.ctx, "videolist").Result()

	var video []*userrpc.VideoList

	if result == "" {
		video = mysql.VideoList(mysql.DB, in)

		marshal, _ := json.Marshal(video)

		cache.Rdb.Set(l.ctx, "videolist", marshal, time.Minute*10)
	} else {
		json.Unmarshal([]byte(result), &video)
	}

	return &userrpc.VideoListResponse{
		List: video,
	}, nil
}
